<?php
/**
 * Description of NewsController
 * @author : Nguyễn Duy Cường
 * @since Oct 19, 2012
 */
class TinnhanController extends AppController {

    public $name = 'Tinnhan';
    public $uses = array('Tinnhan', 'User');
    public $type = 'mess';


    public function beforeFilter() {
        parent::beforeFilter();
        $this->cklogin();
    }
    
    public function add($id = null, $id_mess = null){
        $user_mess = $this->User->findById($id);

        if(!empty($user_mess)){
            $list_all_mess = $this->Tinnhan->find('all', array(
                'fields' => array('id', 'user_id', 'user_id_send', 'status', 'content', 'type'),
                'conditions' => array(
                    'Tinnhan.user_id' => $this->user_login['id']
                ),
                'order' => 'Tinnhan.id DESC',
                'limit' => 50                
            ));
            $this->set('list_all_mess', $list_all_mess);
            $this->set('user_mess', $user_mess);
            if(!empty($this->request->data)){
                if($this->request->data['captcha']!=$_SESSION['captcha']){
                    $this->Session->setFlash('Mã xác nhận không đúng!', 'default', array('class' => 'alert alert-error'));
                    $this->redirect($this->referer());
                    exit;
                }
                if(empty($this->request->data['content'])){
                    $this->Session->setFlash('Bạn phải nhập nội dung tin nhắn !', 'default', array('class' => 'alert alert-error'));
                    $this->redirect($this->referer());
                    exit;
                }
                $data['Tinnhan']['content'] = $this->request->data['content'];
                $data['Tinnhan']['status'] = 0;
                $data['Tinnhan']['user_id'] = $id;
                $data['Tinnhan']['user_id_send'] = $this->user_login['id'];
                $data['Tinnhan']['type'] = $this->type;  
                $this->Tinnhan->create();
                $this->Tinnhan->save($data['Tinnhan']);
                
                if(!empty($id_mess)){
                    $this->Tinnhan->id = $id_mess;
                    $this->Tinnhan->saveField('status', 1);
                }
                
                $this->Session->setFlash('Đã gửi tin nhắn !', 'default', array('class' => 'alert alert-success'));
                $this->redirect($this->referer());
                exit;
            }
        }else{
            if(isset($this->notice['add_failed'])) $this->Session->setFlash($this->notice['add_failed'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
        }
        
        
        
        $this->set('no_backlink','no_backlink');
        $this->set('no_slide', '12345');
        $this->set('title_for_layout', lang('guitinnhancho').' - '.$user_mess['User']['email']);
    }   
    
    /*public function traloi($id = null){
        $user_mess = $this->Tinnhan->findById($id);
        if(!empty($user_mess)){
            $this->set('user_mess', $user_mess);
            if(!empty($this->request->data)){
                if($this->request->data['captcha']!=$_SESSION['captcha']){
                    $this->Session->setFlash('Mã xác nhận không đúng!', 'default', array('class' => 'alert alert-error'));
                    $this->redirect($this->referer());
                    exit;
                }
                if(empty($this->request->data['content'])){
                    $this->Session->setFlash('Bạn phải nhập nội dung tin nhắn !', 'default', array('class' => 'alert alert-error'));
                    $this->redirect($this->referer());
                    exit;
                }
                $data['Tinnhan']['content'] = $this->request->data['content'];
                $data['Tinnhan']['status'] = 0;
                $data['Tinnhan']['user_id'] = $user_mess['Tinnhan']['user_id'];
                $data['Tinnhan']['user_id_send'] = $user_mess['Tinnhan']['user_id_send'];
                $data['Tinnhan']['type'] = $this->type;  
                $this->Tinnhan->save($data['Tinnhan']);
                
                $this->Session->setFlash('Đã gửi tin nhắn !', 'default', array('class' => 'alert alert-success'));
                $this->redirect($this->referer());
                exit;
            }
        }else{
            if(isset($this->notice['add_failed'])) $this->Session->setFlash($this->notice['add_failed'], 'default', array('class' => 'alert alert-error'));
            $this->redirect($this->referer());
        }
        
        $this->set('no_backlink','no_backlink');
        $this->set('no_slide', '12345');
        $this->set('title_for_layout', lang('guitinnhancho').' - '.$user_mess['User']['email']);
    }*/
     
}
